/**
 * Created by stjj on 2022/9/14.
 */

import io.appium.java_client.TouchAction;
import io.appium.java_client.android.Activity;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.touch.WaitOptions;
import io.appium.java_client.touch.offset.PointOption;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeSuite;

import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;
import java.util.Set;

public class Scanningcodeforduty {
    private AndroidDriver driver;
    public  TouchAction touchAction;

    @BeforeSuite
    //夜神：127.0.0.1:62001
    //雷电：emulator:5554
    public void setUp() throws MalformedURLException {
        DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
        desiredCapabilities.setCapability("platformName", "Android");
        desiredCapabilities.setCapability("appium:platformVersion", "7.1.1");
        desiredCapabilities.setCapability("appium:deviceName", "923fcc18");
        desiredCapabilities.setCapability("appium:appPackage", "com.tencent.mm");
        desiredCapabilities.setCapability("appium:appActivity", "com.tencent.mm.ui.LauncherUI");
        desiredCapabilities.setCapability("appium:noReset", true);
        desiredCapabilities.setCapability("appium:automationName", "UiAutomator1");
        desiredCapabilities.setCapability("appium:ensureWebviewsHavePages", true);
        desiredCapabilities.setCapability("appium:nativeWebScreenshot", true);
        desiredCapabilities.setCapability("appium:newCommandTimeout", 3600);
        desiredCapabilities.setCapability("appium:connectHardwareKeyboard", true);
        desiredCapabilities.setCapability("appium:autoLaunch", false);
        // 支持X5内核应用自动化配置
        desiredCapabilities.setCapability("recreateChromeDriverSessions", true);
        ChromeOptions options = new ChromeOptions();
        //com.tencent.mm:appbrand0为前面通过adb命令查询到的小程序进程名，同之前保持一致
        options.setExperimentalOption("androidProcess", "com.tencent.mm:toolsmp");
        desiredCapabilities.setCapability(ChromeOptions.CAPABILITY, options);
        // 初始化会默认将chrome浏览器打开，需要将Browser置为空
        desiredCapabilities.setBrowserName("");
        URL remoteUrl = new URL("http://127.0.0.1:4723/wd/hub");
        driver = new AndroidDriver(remoteUrl, desiredCapabilities);
    }

    @org.testng.annotations.Test
    public void sampleTest() throws InterruptedException {
        try {
            Thread.sleep(5000);
            TouchAction touchAction = new TouchAction(driver);
            touchAction.press(PointOption.point(500, 1685)).waitAction(WaitOptions.waitOptions(Duration.ofMillis(500))).moveTo(PointOption.point(500, 218)).release().perform();
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        TouchAction touchAction = new TouchAction(driver);
        //press：按压某一个坐标 moveTo:滑动到某一个点 release:手指的释放
        Activity activit = new Activity("com.tencent.mm","com.tencent.mm.ui.LauncherUI");
        driver.startActivity(activit);
        Thread.sleep(3000);
        driver.findElementByAndroidUIAutomator("text(\"发现\")").click();
        Thread.sleep(3000);
        driver.findElementByAndroidUIAutomator("text(\"扫一扫\")").click();
        Thread.sleep(3000);
        touchAction.tap(PointOption.point(939, 1572)).release().perform();
        Thread.sleep(3000);
        touchAction.tap(PointOption.point(404, 337)).release().perform();
        Thread.sleep(3000);
        Set<String> contextHandles = driver.getContextHandles();
        System.out.println(contextHandles);
        driver.context("WEBVIEW_com.tencent.mm:toolsmp");
        Set<String> allHandles = driver.getWindowHandles();
        for (String handle:allHandles){
            //通过窗口的标题来判断
            if(driver.getTitle().equals("易车间")){
                break;
            }else {
                //切换句柄
                driver.switchTo().window(handle);
            }
        }
        Thread.sleep(3000);
        driver.findElement(By.xpath("//button[contains(text(),\"上岗\")]")).click();
        Thread.sleep(3000);
        driver.findElement(By.xpath("//a[contains(text(),\"确认\")]")).click();
        Thread.sleep(3000);
        driver.findElement(By.xpath("//button[contains(text(),\"离岗\")]")).click();
        Thread.sleep(3000);

        try {
            Thread.sleep(6000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

    @AfterClass
    public void tearDown() {
        driver.quit();
    }

    public static void main(String[] args) {

    }
}
